package com.yidu.hbatsams.action.emp;

import com.yidu.hbatsams.domain.AtsBaseSport;
import com.yidu.hbatsams.domain.AtsEmpExpert;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.service.emp.AtsEmpExpertService;
import com.yidu.hbatsams.service.UserService;
import com.yidu.hbatsams.service.emp.AtsEmpWordServic;
import com.yidu.hbatsams.utils.Config;
import com.yidu.hbatsams.utils.Message;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 专家管理的控制层
 */
@Controller
@RequestMapping("/UserseControllerExpert")
public class AtsEmpExpertController {
    @Resource
    AtsEmpExpertService atsEmpExpertService;
    @Resource
    AtsBaseSportService atsBaseSportService;
    @Resource
    UserService userService;
    @Resource
    Config config;
    @Resource
    AtsEmpWordServic atsEmpWordServic;

    /**
     * 查询所有
     * @param pageNum
     * @param pageSize
     * @param expertName
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String, Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, String expertName,String startTime,String endTime){
        Map<String,Object> map = new HashMap<>();
        Long count = atsEmpExpertService.count();
        List<AtsEmpExpert> list = atsEmpExpertService.selectAll(pageNum, pageSize, expertName,startTime,endTime);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count); // 总行数
        map.put("data", list);
     return map;
    }

    /**
     * 新增加修改
     * @param record
     * @return
     */
    @ResponseBody
    @RequestMapping("UpdateAndAdd")
    public Message addInfoUpdate(AtsEmpExpert record, HttpSession session){
        Message mes = new Message();
        User userTwo = new User();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        //根据前台得到项目的字符串类型
        int sportName = Integer.valueOf(record.getExpertSpecia());
        //根据项目名称查询项目ID
        AtsBaseSport sport = atsBaseSportService.findById(sportName);
        //设置到ATS参数中
        record.setExpertSpecia(String.valueOf(sport.getSportId()));

        if (record.getExpertId() != null && record.getExpertId() >0){
            UserVo user = (UserVo) session.getAttribute("user");
            AtsEmpExpert expert = atsEmpExpertService.selectByPrimaryKey(record.getExpertId());
            User userThree = atsEmpExpertService.findClerk("3",String.valueOf(expert.getExpertId()));
            userTwo.setUserName(record.getExpertName());
            userTwo.setUserPhone(record.getPhone());
            userTwo.setClerkId(String.valueOf(record.getExpertId()));//设置专家ID
            userTwo.setClerkName(record.getExpertName());
            userTwo.setUserValidity("有效");

            userTwo.setUserTime(dateFormat.format(date));//设置创建时间
            userTwo.setUserPeople(user.getUserName()); //设置创建人
            userTwo.setUserId(userThree.getUserId());
            atsEmpExpertService.updateByPrimaryKeySelective(userTwo);
            record.setCreater(user.getUserName());
            atsEmpExpertService.updateExpertByPrimaryKey(record);
        }else if (record.getExpertId() == null) {
            UserVo user = (UserVo) session.getAttribute("user");
            record.setCreater(user.getUserName());
            atsEmpExpertService.insertExpert(record);

            userTwo.setUserId(uuid);
            userTwo.setUserName(record.getExpertName());
            userTwo.setUserPwd("123456");
            userTwo.setUserPhone(record.getPhone());
            //userTwo.setUserEmail("1046657128@qq.com");
            userTwo.setCompanyId("3");//用来区分表，1 领导，2 教练员，3 专家， 4 运动员
            userTwo.setCompanyName("专家");
            userTwo.setClerkId(String.valueOf(record.getExpertId()));//设置专家ID
            userTwo.setClerkName(record.getExpertName());
            userTwo.setUserValidity("有效");

            userTwo.setUserTime(dateFormat.format(date));//设置创建时间
            userTwo.setUserPeople(user.getUserName()); //设置创建人

            atsEmpExpertService.insert(userTwo);

            mes.setNum(1);
        }
        mes.setMsg("操作成功！");
        return mes;
    }

    /**
     * 根据ID删除
     * @param expertId
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public Message deleteById(Integer expertId){
        Message msg = new Message();
        AtsEmpExpert expert = atsEmpExpertService.selectByPrimaryKey(expertId);
        atsEmpExpertService.deleteClerk("3",String.valueOf(expert.getExpertId()));
        atsEmpExpertService.deleteByPrimaryKey(expertId);

        msg.setNum(1);
        msg.setMsg("删除成功！");
        return msg;
    }

    /**
     * 批量删除
     * @param expertIds
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteMany")
    public Message deleteMoreById(String expertIds){
        String[] ids = null;
        Message msg = new Message();
        msg.setNum(1);
        //如果为批量删除
        if(expertIds!=null && !"".equals(expertIds)){
            ids=expertIds.split(",");
        }
        for (int i = 0; i < ids.length; i++){
            String expertId = ids[i];
            atsEmpExpertService.deleteClerk("3",String.valueOf(expertId));
            atsEmpExpertService.deleteByPrimaryKey(Integer.valueOf(expertId));
        }
        msg.setMsg("删除成功！");
        return msg;
    }

    /**
     * 修改显示
     * @param expertId
     * @return
     */
    @ResponseBody
    @RequestMapping("select")
    public AtsEmpExpert selectById(Integer expertId){
        return atsEmpExpertService.selectByPrimaryKey(expertId);
    }
    /**
     * 查询项目名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findBaseSportName")
    public List<AtsBaseSport> findBySportName(){
        return atsBaseSportService.findAllSportName();
    }

    /**
     * 创建word文档的方法
     * @param response
     * @param id
     * @param type
     * @param user
     * @param session
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("exportWord")
    public void exportWord(HttpServletResponse response, Integer id, Integer type, User user, HttpSession session) throws IOException {
        //查询所需数据
        OutputStream os = response.getOutputStream();
        String contextPath = config.getDomainUrl();

        response.setContentType("application/vnd.ms-word");
        XWPFDocument document= atsEmpWordServic.createWord(type,id,response,user,contextPath,session);
        document.write(os);

        document.close();

        os.flush();

        os.close();

    }
}
